home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 4111 / 4111.xpi / content / aardvarkMain.js < prev    next >
Text File  |  2010-02-05  |  13KB  |  464 lines

  1. aardvark.loadObject ({
  2.  
  3. //-------------------------------------------------
  4. showHelpTip : function () {
  5. var dbox = new AardvarkDBox ("#fff2db", false, true, true);
  6. dbox.innerContainer.innerHTML = "<p style='clear: both; margin: 3px 0 0 0;'><img src='" +  this.resourcePrefix + "aardvarkhelp.gif' style=' float: right; margin: 0 0 0px 0'>" + this.strings.initialTipText + "</p>";
  7. dbox.innerContainer.style.width = "14em";
  8. dbox.innerContainer.style.height = "54px";
  9. dbox.show ();
  10. setTimeout ("aardvark.killDbox(" + dbox.id + ")", 2000);
  11. return true;
  12. },
  13.  
  14. //-------------------------------------------------
  15. // create the box and tag etc (done once and saved)
  16. makeElems : function () {
  17. this.borderElems = [];
  18. var d, i, s;
  19.  
  20. for (i=0; i<4; i++) {
  21.   d = this.doc.createElement ("DIV");
  22.   s = d.style;
  23.   s.display = "none";
  24.   s.overflow = "hidden";
  25.   s.position = "absolute";
  26.   s.height = "2px";
  27.   s.width = "2px";
  28.   s.top = "20px";
  29.   s.left = "20px";
  30.   s.zIndex = "5001";
  31.   d.isAardvark = true; // mark as ours
  32.   this.borderElems[i] = d;
  33.   this.doc.body.appendChild (d);
  34.   }
  35. var be = this.borderElems;
  36. be[0].style.borderTopWidth = "2px";
  37. be[0].style.borderTopColor = "#f00";
  38. be[0].style.borderTopStyle = "solid";
  39. be[1].style.borderBottomWidth = "2px";
  40. be[1].style.borderBottomColor = "#f00";
  41. be[1].style.borderBottomStyle = "solid";
  42. be[2].style.borderLeftWidth = "2px";
  43. be[2].style.borderLeftColor = "#f00";
  44. be[2].style.borderLeftStyle = "solid";
  45. be[3].style.borderRightWidth = "2px";
  46. be[3].style.borderRightColor = "#f00";
  47. be[3].style.borderRightStyle = "solid";
  48.  
  49. d = this.doc.createElement ("DIV");
  50. this.setElementStyleDefault (d, "#fff0cc");
  51. d.isAardvark = true; // mark as ours
  52. d.isLabel = true; // 
  53. d.style.borderTopWidth = "0";
  54. d.style.MozBorderRadiusBottomleft = "6px";
  55. d.style.MozBorderRadiusBottomright = "6px";
  56. d.style.zIndex = "5005";
  57. d.style.visibility = "hidden";
  58. this.doc.body.appendChild (d);
  59. this.labelElem = d;
  60.  
  61. d = this.doc.createElement ("DIV");
  62. this.setElementStyleDefault (d, "#dfd");
  63. d.isAardvark = true; // mark as ours
  64. d.isKeybox = true; // 
  65. d.style.backgroundColor = "#cfc";
  66. d.style.zIndex = "5006";
  67. this.doc.body.appendChild (d);
  68. this.keyboxElem = d;
  69. },
  70.  
  71. //-------------------------------------------------
  72. // show the red box around the element, and display
  73. // the string in the little tag
  74. showBoxAndLabel : function (elem, string) {
  75. var pos = this.getPos(elem)
  76. var dims = this.getWindowDimensions ();
  77. var y = pos.y;
  78.  
  79. this.moveElem (this.borderElems[0], pos.x, y);
  80. this.borderElems[0].style.width = elem.offsetWidth + "px";
  81. this.borderElems[0].style.display = "";
  82.  
  83. this.moveElem (this.borderElems[1], pos.x, y+elem.offsetHeight-2);
  84. this.borderElems[1].style.width = (elem.offsetWidth + 2)  + "px";
  85. this.borderElems[1].style.display = "";
  86.  
  87. this.moveElem (this.borderElems[2], pos.x, y);
  88. this.borderElems[2].style.height = elem.offsetHeight  + "px";
  89. this.borderElems[2].style.display = "";
  90.  
  91. this.moveElem (this.borderElems[3], pos.x+elem.offsetWidth-2, y);
  92. this.borderElems[3].style.height = elem.offsetHeight + "px";
  93. this.borderElems[3].style.display = "";
  94.  
  95. y += elem.offsetHeight + 2;
  96.  
  97. this.labelElem.innerHTML = string;
  98. this.labelElem.style.display = '';
  99.  
  100. // adjust the label as necessary to make sure it is within screen and
  101. // the border is pretty
  102. if ((y + this.labelElem.offsetHeight) >= dims.scrollY + dims.height) {
  103.   this.labelElem.style.borderTopWidth = "1px";
  104.   this.labelElem.style.MozBorderRadiusTopleft = "6px";
  105.   this.labelElem.style.MozBorderRadiusTopright = "6px";
  106.   this.labelDrawnHigh = true;
  107.   y = (dims.scrollY + dims.height) - this.labelElem.offsetHeight;
  108.   }
  109. else if (this.labelElem.offsetWidth > elem.offsetWidth) {
  110.   this.labelElem.style.borderTopWidth = "1px";
  111.   this.labelElem.style.MozBorderRadiusTopright = "6px";
  112.   this.labelDrawnHigh = true;
  113.   }
  114. else if (this.labelDrawnHigh) {
  115.   this.labelElem.style.borderTopWidth = "0";
  116.   this.labelElem.style.MozBorderRadiusTopleft = "";
  117.   this.labelElem.style.MozBorderRadiusTopright = "";
  118.   delete (this.labelDrawnHigh); 
  119.   }
  120. this.moveElem (this.labelElem, pos.x+2, y);
  121. this.labelElem.style.visibility = "visible";
  122. },
  123.  
  124. //-------------------------------------------------
  125. removeBoxFromBody : function () {
  126. if (this.labelElem) {
  127.   this.doc.body.removeChild(this.labelElem);
  128.   this.labelElem = null;
  129.   }
  130. if (this.keyboxElem) {
  131.   this.doc.body.removeChild(this.keyboxElem);
  132.   this.keyboxElem = null;
  133.   }
  134. if (this.borderElems != null) {
  135.   for (var i=0; i<4; i++)
  136.     this.doc.body.removeChild(this.borderElems[i]);
  137.   this.borderElems = null;
  138.   }
  139. },
  140.  
  141. //-------------------------------------------------
  142. // remove the red box and tag
  143. clearBox : function () {
  144. this.selectedElem = null;
  145. if (this.borderElems != null) {
  146.   for (var i=0; i<4; i++)
  147.     this.borderElems[i].style.display = "none";
  148.   this.labelElem.style.display = "none";
  149.   this.labelElem.style.visibility = "hidden";
  150.   }
  151. },
  152.  
  153. //-------------------------------------------------
  154. hideKeybox : function () {
  155. this.keyboxElem.style.display = "none";
  156. this.keyboxTimeoutHandle = null;
  157. },
  158.  
  159. //-------------------------------------------------
  160. showKeybox : function (command){
  161. if (this.keyboxElem == null)
  162.   return;
  163.   
  164. if (command.keyOffset >= 0) {
  165.   var s1 = command.name.substring(0, command.keyOffset);
  166.   var s2 = command.name.substring(command.keyOffset+1);
  167.   
  168.   this.keyboxElem.innerHTML = s1 + "<b style='font-size:2em;'>" +
  169.       command.name.charAt(command.keyOffset) + "</b>" + s2;
  170.   }
  171. else {
  172.   this.keyboxElem.innerHTML = command.name;
  173.   }
  174.   
  175. var dims = this.getWindowDimensions ();
  176. var y = dims.scrollY + this.mousePosY + 10;
  177. if (y < 0)
  178.   y = 0;
  179. else if (y > (dims.scrollY + dims.height) - 30)
  180.   y = (dims.scrollY + dims.height) - 60;
  181. var x = this.mousePosX + 10;
  182. if (x < 0)
  183.   x = 0;
  184. else if (x > (dims.scrollX + dims.width) - 60)
  185.   x = (dims.scrollX + dims.width) - 100;
  186.  
  187. this.moveElem (this.keyboxElem, x, y);
  188. this.keyboxElem.style.display = "";
  189. if (this.keyboxTimeoutHandle)
  190.   clearTimeout (this.keyboxTimeoutHandle);
  191. this.keyboxTimeoutHandle = setTimeout ("aardvark.hideKeybox()", 400);
  192. },
  193.  
  194. validIfBlockElements : {
  195.   SPAN: 1,
  196.   A: 1
  197.   },
  198.  
  199. validIfNotInlineElements : {
  200.   UL: 1,
  201.   LI: 1,
  202.   OL: 1,
  203.   PRE: 1,
  204.   CODE: 1
  205.   },
  206.  
  207. alwaysValidElements : {
  208.   DIV: 1,
  209.   IFRAME: 1,
  210.   OBJECT: 1,
  211.   APPLET: 1,
  212.   BLOCKQUOTE: 1,
  213.   H1: 1,
  214.   H2: 1,
  215.   H3: 1,
  216.   FORM: 1,
  217.   P: 1,
  218.   TABLE: 1,
  219.   TD: 1,
  220.   TH: 1,
  221.   TR: 1,
  222.   IMG: 1
  223.   },
  224.  
  225. //-------------------------------------------------
  226. // given an element, walk upwards to find the first
  227. // valid selectable element
  228. findValidElement : function (elem) {
  229. while (elem) {
  230.   if (this.alwaysValidElements[elem.tagName])
  231.     return elem;
  232.   else if (this.validIfBlockElements[elem.tagName]) {
  233.     if (this.doc.defaultView) {
  234.       if (this.doc.defaultView.getComputedStyle
  235.             (elem, null).getPropertyValue("display") == 'block')
  236.         return elem;
  237.       }
  238.     else if (elem.currentStyle){
  239.       if (elem.currentStyle["display"] == 'block')
  240.         return elem;   
  241.       }
  242.     }
  243.   else if (this.validIfNotInlineElements[elem.tagName]){
  244.     if (this.doc.defaultView) {
  245.       if (this.doc.defaultView.getComputedStyle
  246.             (elem, null).getPropertyValue("display") != 'inline')
  247.         return elem;
  248.       }
  249.     else if (elem.currentStyle) {
  250.       if (elem.currentStyle["display"] != 'inline')
  251.         return elem;   
  252.       }
  253.     }
  254.   elem = elem.parentNode;
  255.   }
  256. return elem;
  257. },
  258.  
  259. //-------------------------------------------------
  260. makeElementLabelString : function (elem) {
  261. var s = "<b style='color:#000'>" + elem.tagName.toLowerCase() + "</b>";
  262. if (elem.id != '')
  263.   s += ", id: " + elem.id;
  264. if (elem.className != '')
  265.   s += ", class: " + elem.className;
  266. return s;
  267. },
  268.  
  269. //-------------------------------------------------
  270. mouseUp : function (evt) {
  271. // todo: remove all this when we replace dlogbox with our popupwindow
  272. if (aardvark.dragElement) {
  273.   delete aardvark.dragElement;
  274.   delete aardvark.dragClickX;
  275.   delete aardvark.dragClickY;
  276.   delete aardvark.dragStartPos;
  277.   }
  278. return false;
  279. },
  280.  
  281. // the following three functions are the main event handlers
  282. // note: "this" does not point to aardvark.main in these
  283. //-------------------------------------------------
  284. mouseMove : function (evt) {
  285. if (!evt)
  286.   evt = aardvark.window.event;
  287.  
  288. if (aardvark.mousePosX == evt.clientX &&
  289.       aardvark.mousePosY == evt.clientY) {
  290.   aardvark.mouseMoved = false;
  291.   return;
  292.   }
  293.  
  294. // todo: remove all this when we replace dlogbox with our popupwindow
  295. aardvark.mousePosX  = evt.clientX;
  296. aardvark.mousePosY = evt.clientY;
  297.  
  298. if (aardvark.dragElement) {
  299.   aardvark.moveElem (aardvark.dragElement, 
  300.       (aardvark.mousePosX - aardvark.dragClickX) + aardvark.dragStartPos.x,
  301.       (aardvark.mousePosY - aardvark.dragClickY) + aardvark.dragStartPos.y);
  302.   aardvark.mouseMoved = false;
  303.   return true;
  304.   }
  305.  
  306. // if it hasn't actually moved (for instance, if something 
  307. // changed under it causing a mouseover), we want to know that
  308. aardvark.mouseMoved = true;
  309. return false;
  310. },
  311.  
  312. //-------------------------------------------------
  313. mouseOver : function (evt) {
  314. if (!evt)
  315.   evt = aardvark.window.event;
  316.  
  317. if (!aardvark.mouseMoved)
  318.   return;
  319.  
  320. var elem = aardvark.getElemFromEvent (evt);
  321. if (elem == null) {
  322.   aardvark.clearBox ();
  323.   return;
  324.   }
  325. elem = aardvark.findValidElement (elem);
  326.  
  327. if (elem == null) {
  328.   aardvark.clearBox();
  329.   return;
  330.   }
  331.   
  332. // note: this assumes that:
  333. // 1. our display elements would be selectable types, and
  334. // 2. elements inside display elements would not
  335. if (elem.isAardvark) {
  336.   if (elem.isKeybox)
  337.     aardvark.hideKeybox();
  338.   else if (elem.isLabel)
  339.     aardvark.clearBox();
  340.   else
  341.     aardvark.isOnAardvarkElem = true;
  342.   return;
  343.   }
  344.  
  345. // this prevents it from snapping back to another element
  346. // if you do a "wider" or "narrower" while on top of one
  347. // of the border lines.  not fond of this, but its about
  348. // the best i can do
  349. if (aardvark.isOnAardvarkElem && aardvark.didWider) {
  350.   var e = elem, foundIt = false;
  351.   while ((e = e.parentNode) != null) {
  352.     if (e == aardvark.selectedElem) {
  353.       foundIt = true;
  354.       break;
  355.       }
  356.     }
  357.   if (foundIt) {
  358.     aardvark.isOnAardvarkElem = false;
  359.     return;
  360.     }
  361.   }
  362. aardvark.isOnAardvarkElem = false;
  363. aardvark.didWider = false;
  364.   
  365. if (elem == aardvark.selectedElem)
  366.   return;
  367. aardvark.widerStack = null;
  368. aardvark.selectedElem = elem;
  369. aardvark.showBoxAndLabel (elem, aardvark.makeElementLabelString (elem));
  370. aardvark.mouseMoved = false;
  371. },
  372.  
  373. //-------------------------------------------------
  374. keyDown : function (evt) {
  375. if (!evt)
  376.   evt = aardvark.window.event;
  377. var c;
  378.  
  379. if (evt.ctrlKey || evt.metaKey || evt.altKey)
  380.   return true;
  381.  
  382. var keyCode = evt.keyCode ? evt.keyCode :
  383.       evt.charCode ? evt.charCode :
  384.       evt.which ? evt.which : 0;
  385. c = String.fromCharCode(keyCode).toLowerCase();
  386. var command = aardvark.getByKey(c);
  387.  
  388. if (command) {
  389.   if (command.noElementNeeded) {
  390.     if (command.func.call (aardvark) == true)
  391.       aardvark.showKeybox (command);
  392.     }
  393.   else {
  394.     if (aardvark.selectedElem && 
  395.         (command.func.call (aardvark, aardvark.selectedElem) == true))
  396.       aardvark.showKeybox (command);
  397.     }
  398.   }
  399. if (c < 'a' || c > 'z')
  400.   return true;
  401. if (evt.preventDefault)
  402.   evt.preventDefault ();
  403. else
  404.   evt.returnValue = false; 
  405. return false;
  406. },
  407.  
  408. //-------------------------------------------------
  409. // this is the main entry point when starting aardvark
  410. start : function () {
  411. this.loadCommands();
  412.  
  413. if (this.isBookmarklet) {
  414.   this.window = window;
  415.   this.doc = document;
  416.   }
  417. else {
  418.   this.doc = ((gContextMenu) ? gContextMenu.target.ownerDocument : window._content.document);
  419.   this.window = window._content;
  420.   }
  421.  
  422. if (this.doc.aardvarkRunning) {
  423.   this.quit();
  424.   return;
  425.   }
  426. else {
  427.   this.makeElems (); 
  428.   this.selectedElem = null;
  429.   
  430.   // need this to be page specific (for extension)...if you 
  431.   // change the page, aardvark will not be running
  432.   this.doc.aardvarkRunning = true;
  433.   
  434.   if (this.doc.all) {
  435.     this.doc.attachEvent ("onmouseover", this.mouseOver);
  436.     this.doc.attachEvent ("onmousemove", this.mouseMove);
  437.     this.doc.attachEvent ("onmouseup", this.mouseUp);
  438.     this.doc.attachEvent ("onkeypress", this.keyDown);
  439.     }
  440.   else {
  441.     this.doc.addEventListener ("mouseover", this.mouseOver, false);
  442.     this.doc.addEventListener ("mouseup", this.mouseUp, false);
  443.     this.doc.addEventListener ("mousemove", this.mouseMove, false);
  444.     this.doc.addEventListener ("keypress", this.keyDown, false);
  445.     }
  446.  
  447.   // show tip if its been more than an hour
  448.   if (!this.isBookmarklet) {
  449.     var t = new Date().getTime()/(1000*60);
  450.     var diff = t - this.tipLastShown;
  451.     if (diff > 60) { // more than an hour
  452.       this.tipLastShown = Math.round(t);
  453.       this.prefManager.setIntPref("extensions.aardvark@rob.brown.tipLastShown",
  454.         this.tipLastShown);
  455.       this.showHelpTip();
  456.       } 
  457.     }
  458.   } 
  459. }
  460. });
  461.  
  462.  
  463.  
  464.